package com.adinnet.admin.schoolWebsite.service.impl;

import com.adinnet.admin.address.mapper.AddressMapper;
import com.adinnet.admin.address.model.Address;
import com.adinnet.admin.contact.model.vo.ContactResp;
import com.adinnet.admin.productImg.model.ProductImg;
import com.adinnet.admin.schoolWebsite.model.SchoolWebsite;
import com.adinnet.admin.schoolWebsite.mapper.SchoolWebsiteMapper;
import com.adinnet.admin.schoolWebsite.model.vo.SchoolWebsiteReq;
import com.adinnet.admin.schoolWebsite.service.SchoolWebsiteService;
import com.adinnet.admin.schoolWebsiteImg.mapper.SchoolWebsiteImgMapper;
import com.adinnet.admin.schoolWebsiteImg.model.SchoolWebsiteImg;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 校区网点 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-16
 */
@Service
public class SchoolWebsiteServiceImpl extends ServiceImpl<SchoolWebsiteMapper, SchoolWebsite> implements SchoolWebsiteService {


    @Autowired
    private SchoolWebsiteMapper schoolWebsiteMapper;

    @Autowired
    private SchoolWebsiteImgMapper schoolWebsiteImgMapper;

    @Autowired
    private AddressMapper addressMapper;

    /**
     * @param keyword:
     * @Description: 分校区网点列表
     * @author zYshuai
     * @Date: 2020/12/16 16:59
     * @return: java.util.List<com.adinnet.admin.schoolWebsite.model.SchoolWebsite>
     */
    @Override
    public PageResult<SchoolWebsiteReq> schoolWebsiteList(String keyword, Integer page, Integer limit) {
        Page<SchoolWebsiteReq> ipage = new Page<SchoolWebsiteReq>(page, limit);
        List<SchoolWebsiteReq> schoolWebsiteReqs = schoolWebsiteMapper.schoolWebsiteList(ipage,keyword);
//        IPage<SchoolWebsiteReq> websiteIPage = schoolWebsiteMapper.selectPage(ipage, new QueryWrapper<SchoolWebsite>().eq("deleted", 0).orderByDesc("created_date"));
        if ( CollectionUtils.isNotEmpty(schoolWebsiteReqs)) {
            for (SchoolWebsiteReq record : schoolWebsiteReqs) {
                record.setProvinceNames((StringUtils.isNotBlank(record.getProvinceName()) ? record.getProvinceName() : "") + (StringUtils.isNotBlank(record.getCityName()) ? record.getCityName() : "") + (StringUtils.isNotBlank(record.getAreaName()) ? record.getAreaName() : ""));
                Address provinceAddress = addressMapper.getAddressById(record.getProvinceId());
                record.setProvinceId((null != provinceAddress && StringUtils.isNotEmpty(provinceAddress.getCode())) ? Integer.parseInt(provinceAddress.getCode()) : 0);
                Address cityAddress = addressMapper.getAddressById(record.getCityId());
                record.setCityId((null != cityAddress && StringUtils.isNotEmpty(cityAddress.getCode())) ? Integer.parseInt(cityAddress.getCode()) : 0);
                Address areaAddress = addressMapper.getAddressById(record.getAreaId());
                record.setAreaId((null != areaAddress && StringUtils.isNotEmpty(areaAddress.getCode())) ? Integer.parseInt(areaAddress.getCode()) : 0);
                record.setSchoolWebsiteImages(schoolWebsiteImgMapper.getImgs(record.getId()));
            }
        }
        return new PageResult<>(schoolWebsiteReqs, ipage.getTotal());
    }

    /**
     * @param schoolWebsiteId:
     * @Description: 删除分校区网点
     * @author zYshuai
     * @Date: 2020/12/16 16:59
     * @return: com.adinnet.core.JsonResult
     */
    @Override
    public JsonResult delete(Integer schoolWebsiteId) {
        schoolWebsiteMapper.delSchoolWebsite(schoolWebsiteId);
        schoolWebsiteImgMapper.delSchoolWebsiteImg(schoolWebsiteId);
        return JsonResult.ok("删除成功!");
    }

    /**
     * @param schoolWebsiteReq:
     * @Description: 添加分校区
     * @author zYshuai
     * @Date: 2020/12/16 17:03
     * @return: boolean
     */
    @Override
    public boolean add(SchoolWebsiteReq schoolWebsiteReq) {
        schoolWebsiteMapper.insert(schoolWebsiteReq);
        if (StringUtils.isNotEmpty(schoolWebsiteReq.getSchoolWebsiteImages())) {
            String[] images = schoolWebsiteReq.getSchoolWebsiteImages().split(",");
            for (String image : images) {
                SchoolWebsiteImg img = new SchoolWebsiteImg();
                img.setImgUrl(image);
                img.setSchoolWebsiteId(schoolWebsiteReq.getId());
                schoolWebsiteImgMapper.insert(img);
            }
        }
        return true;
    }

    /**
     * @param schoolWebsiteReq:
     * @Description: 修改分校区
     * @author zYshuai
     * @Date: 2020/12/16 17:03
     * @return: boolean
     */
    @Override
    public boolean updateSchoolWebsite(SchoolWebsiteReq schoolWebsiteReq) {
        schoolWebsiteMapper.updateById(schoolWebsiteReq);
        schoolWebsiteImgMapper.delSchoolWebsiteImg(schoolWebsiteReq.getId());
        if (StringUtils.isNotEmpty(schoolWebsiteReq.getSchoolWebsiteImages())) {
            for (String image : schoolWebsiteReq.getSchoolWebsiteImages().split(",")) {
                SchoolWebsiteImg img = new SchoolWebsiteImg();
                img.setImgUrl(image);
                img.setSchoolWebsiteId(schoolWebsiteReq.getId());
                schoolWebsiteImgMapper.insert(img);
            }
        }
        return true;
    }
}
